package org.springframework.samples.countries.web;

import java.util.HashMap;
import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import pikes.ecma.Addition;
import pikes.ecma.Assignment;
import pikes.ecma.FunctionDeclaration;
import pikes.ecma.Identifier;
import pikes.ecma.MemberPropertyAccess;
import pikes.ecma.SourceElement;
import pikes.ecma.StringLiteral;
import pikes.ecma.VariableStatement;
import pikes.peak.InternalURI;
import pikes.peak.URIParameterBuilder;
import pikes.peak.test.PikesPeakComponentTestCase;
import pikes.samples.countries.ui.peak.DynamicAddition;
import pikes.samples.countries.ui.peak.DynamicFunctionDeclaration;
import pikes.samples.countries.ui.peak.DynamicVariableStatement;

public class DynamicFunctionDeclarationTestCase extends PikesPeakComponentTestCase {

	InternalURI detailPageURI = new InternalURI("/main/detail.htm", new URIParameterBuilder() {

		@Override
		public void build(Map<CharSequence, CharSequence> parameters) {
			parameters.put("code", "");
		}

	});

	@Test
	public void functionDeclaration() throws Exception {
		DynamicFunctionDeclaration funcDecl = new DynamicFunctionDeclaration("go");

		SourceElement code = funcDecl.getValue(new HashMap(), pikesPeakRequest.getRequestContext());

		Assert.assertEquals(new FunctionDeclaration("go"), code);
	}

	@Test
	public void functionDeclaration1() throws Exception {
		DynamicFunctionDeclaration funcDecl = new DynamicFunctionDeclaration("go");
		funcDecl.addFormalParameter("boo");

		SourceElement code = funcDecl.getValue(new HashMap(), pikesPeakRequest.getRequestContext());

		FunctionDeclaration expected = new FunctionDeclaration("go");
		expected.add("boo");
		Assert.assertEquals(expected, code);
	}

	@Test
	public void functionDeclaration2() throws Exception {
		DynamicFunctionDeclaration funcDecl = new DynamicFunctionDeclaration("go");
		funcDecl.addFormalParameter("Some text");

		SourceElement code = funcDecl.getValue(new HashMap(), pikesPeakRequest.getRequestContext());

		FunctionDeclaration expected = new FunctionDeclaration("go");
		expected.add("Some text");
		Assert.assertEquals(expected, code);
	}

	@Test
	public void functionDecl() throws Exception {
		DynamicFunctionDeclaration dynfuncDecl = new DynamicFunctionDeclaration("go");
		dynfuncDecl.addFormalParameter("country");

		DynamicVariableStatement dynvarDec = new DynamicVariableStatement("url", new DynamicAddition(detailPageURI,
				"country"));
		dynfuncDecl.addCode(dynvarDec);

		dynfuncDecl.addCode(new Assignment(new MemberPropertyAccess("window", "location"), "url"));

		FunctionDeclaration expected = new FunctionDeclaration("go");
		expected.add("country");

		String url = "url";
		VariableStatement varDec = new VariableStatement(url, new Addition(new StringLiteral(
				"/countries/main/detail.htm?code="), new Identifier("country")));
		expected.add(varDec);
		expected.add(new Assignment(new MemberPropertyAccess("window", "location"), url));

		Assert.assertEquals(expected, dynfuncDecl.getValue(new HashMap(), pikesPeakRequest.getRequestContext()));
	}
}
